package com.carleasoft.mps.forum.business.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.IpUtils;
import com.carleasoft.mps.forum.business.answer.bean.ForumData;
import com.carleasoft.mps.forum.business.answer.bean.ForumPlate;
import com.carleasoft.mps.forum.business.answer.bean.ForumUserPermission;
import com.carleasoft.mps.forum.business.answer.bean.po.ForumDataPo;
import com.carleasoft.mps.forum.business.answer.bean.po.ForumUserPo;
import com.carleasoft.mps.forum.business.answer.bean.vo.ForumUserVo;
import com.carleasoft.mps.forum.business.answer.mapper.ForumDataMapper;
import com.carleasoft.mps.forum.business.answer.service.ForumDataService;
import com.carleasoft.mps.forum.business.answer.service.ForumPlateService;
import com.carleasoft.mps.forum.business.answer.service.ForumUserPermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zang
 * @since 2023-05-09
 */
@Service
public class ForumDataServiceImpl extends ServiceImpl<ForumDataMapper, ForumData> implements ForumDataService {

    @Autowired
    private ForumPlateService forumPlateService;
    @Autowired
    private ForumUserPermissionService forumUserPermissionService;

    @Override
    public Page<ForumData> pageForumData(Page page, ForumDataPo forumDataPo) {
        Page<ForumData> res = this.getBaseMapper().pageForumData(page,forumDataPo);
        List<ForumData> records = res.getRecords();
        for (ForumData record : records) {
            if(null ==  record.getCommentNum()){
                record.setCommentNum(0);
            }
            if(null ==  record.getViewNum()){
                record.setViewNum(0);
            }
        }
        res.setRecords(records);
        return res;
    }
    @Override
    public Page<ForumData> bisPageForumData(Page page, ForumDataPo forumDataPo) {
        Page<ForumData> res = this.getBaseMapper().bisPageForumData(page,forumDataPo);
        return res;
    }
    @Override
    public String saveForumData(ForumData forumData, HttpServletRequest request) {
        String plateCode = forumData.getPlateCode();
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        String ipAddr = IpUtils.getIpAddr(request);
        // 获取用户行为信息
        ForumUserPermission forumUser = forumUserPermissionService.getOne(new QueryWrapper<ForumUserPermission>().eq("USER_CODE", user.getUserCode()), false);
        // 禁止发帖
        if(null != forumUser && forumUser.getIsSubmit() == 1) {
            return "您已被禁止发布论坛信息";
        }
        if(StringUtils.isNotEmpty(plateCode)){
            ForumPlate plate = forumPlateService
                    .getOne(new QueryWrapper<ForumPlate>().eq("PLATE_CODE", plateCode), false);
            if(null != plate){
                Integer isApprove = plate.getIsApprove();
                if(isApprove == 1){
                    /*开启审核，发布状态修改为 待审核*/
                    forumData.setStatus(1);
                } else {
                    /*不需要审核，发布状态修改为 未审核 */
                    forumData.setStatus(2);
                }
                forumData.setArticleCode(UUID.randomUUID().toString());
                forumData.setIsTop(0);
                forumData.setIsEssence(0);
                forumData.setViewNum(0);
                forumData.setIsDelete(0);
                forumData.setIpAddress(ipAddr);

                forumData.setCreatePersonName(user.getUserName());
                forumData.setCreatePersonCode(user.getUserCode());
                forumData.setCreateTime(new Date());
                this.save(forumData);
            }
            return "提交成功";
        }
        else {
            return "所选板块信息不存在";
        }
    }

    @Override
    public void deleteForumData(ForumDataPo forumDataPo) {
        List<String> articleCodes = forumDataPo.getArticleCodes();
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        this.update(new UpdateWrapper<ForumData>()
                .set("IS_DELETE",1)
                .set("UPDATE_PERSON_CODE",user.getUserCode())
                .set("UPDATE_PERSON_NAME",user.getUserName())
                .set("UPDATE_TIME",new Date())
                .in("ARTICLE_CODE",articleCodes));
    }

    @Override
    public void passForumData(ForumDataPo forumDataPo) {
        List<String> articleCodes = forumDataPo.getArticleCodes();
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        this.update(new UpdateWrapper<ForumData>()
                .set("STATUS",2)
                .set("UPDATE_PERSON_CODE",user.getUserCode())
                .set("UPDATE_PERSON_NAME",user.getUserName())
                .set("UPDATE_TIME",new Date())
                .in("ARTICLE_CODE",articleCodes));
    }

    @Override
    public void failForumData(ForumDataPo forumDataPo) {
        List<String> articleCodes = forumDataPo.getArticleCodes();
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        this.update(new UpdateWrapper<ForumData>()
                .set("STATUS",3)
                .set("UPDATE_PERSON_CODE",user.getUserCode())
                .set("UPDATE_PERSON_NAME",user.getUserName())
                .set("UPDATE_TIME",new Date())
                .in("ARTICLE_CODE",articleCodes));
    }

    @Override
    public ForumData detailForumData(ForumDataPo forumDataPo) {
        ForumData res =  this.getBaseMapper().detailForumData(forumDataPo.getArticleCode());
        /*浏览次数加一*/
        res.setViewNum(res.getViewNum() + 1);
        this.updateById(res);
        return res;
    }

    @Override
    public Page<ForumData> selForumTab(Page page, ForumDataPo forumDataPo) {
        Page<ForumData> res = this.getBaseMapper().selForumTab(page,forumDataPo);
        return res;
    }

    @Override
    public ForumUserVo selForumUser(ForumUserPo forumUserPo) {
//        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        ForumUserVo res =  this.getBaseMapper().selForumUser(forumUserPo.getUserCode());
        return res;
    }


}
